<?php 

namespace app\manager\controller;
use think\Loader;
/**
 * 订单管理
 */
class Order extends \think\Controller
{
	use \app\common\traits\controller\TraitController;
	const ORDER_INITIAL_STAGE = 0;//未支付订单
    const ORDER_PAYMENT_STAGE = 1;//订单支付
    const ORDER_REFUND_STAGE  = 2;//订单退款
    const ORDER_COMPLETION    = 3;//订单完成
    const ORDER_CANCEL_STAGE  = 4;//订单取消
    const ORDER_CHECKIN_STAGE = 5;//入住订单
    const START_STATE         = 0;
    const STATE_END           = 1;
    //	'01'  => '等待支付',
	//  '10'  => '继续支付',
	//  '11'  => '已支付订单',
	//  '20'  => '订单退款中',
	//  '21'  => '已退款订单',
	//  '31'  => '订单完成',
	//  '41'  => '已取消订单'
	//	'50'  => '入住中'
	protected function _initialize(){
		if (!model('CampManagers')->checkLogin()) {
            $this->redirect('@Campmanager/login');
            exit;
        }
		$this->model = Loader::model('Orders');
		$this->camp_id = Loader::model('CampManagers')->getLoginUserInfo('camp_id');
		$this->list_route = url('/orders');
	}
	
	/**
	 * 待付款订单信息
	 */
	public function getIndexPay(){
		$query = input('get.');
		$continuePayOrder = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_INITIAL_STAGE, 'status' => self::STATE_END, 'camp_id' => $this->camp_id]));
		$waitPayOrder = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_PAYMENT_STAGE, 'status' => self::START_STATE, 'camp_id' => $this->camp_id]));
	    $order_num = count($continuePayOrder) + count($waitPayOrder);
	    return view('order/index_pay',['continuePayOrder'=>$continuePayOrder, 'order_num'=>$order_num, 'waitPayOrder' => $waitPayOrder]);
	}
	
	/**
	 * 待入住订单信息
	 */
	public function getIndexWaitIn(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_PAYMENT_STAGE, 'status' => self::STATE_END, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/index_wait_in',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 入住中订单信息
	 */
	public function getIndexIn(){
	    $query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_CHECKIN_STAGE, 'status' => self::START_STATE, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/index_in',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 已完成订单信息
	 */
	public function getIndexComplete(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_COMPLETION, 'status' => self::STATE_END, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/index_complete',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 已取消的订单
	 */
	public function getIndexCancel(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_CANCEL_STAGE, 'status' => self::STATE_END, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/index_cancel',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 退款中的订单
	 */
	public function getRefundOn(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_REFUND_STAGE, 'status' => self::START_STATE, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/refund_on',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 已退款订单
	 */
	public function getRefunded(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['stage' => self::ORDER_REFUND_STAGE, 'status' => self::STATE_END, 'camp_id' => $this->camp_id]));
	    $order_num = count($order);
	    return view('order/refunded',['order'=>$order,'order_num'=>$order_num]);
	}
	
	/**
	 * 订单详情
	 */
	public function getRead(){
		$order = $this->model->getOrderInfo($this->request->get('number')); 
		$leave_car_date=strtotime($order['leave_car_date']);
        $take_car_date=strtotime($order['take_car_date']);
        $date=(($leave_car_date-$take_car_date)/86400)+1;
	    return view('order/read',['order' => $order,'date'=>$date]);
	} 
	 
	/**
	 * 改变-->待入住状态为-->入住中状态
	 */
	public function getChangeStayInOrder(){
		$result = Loader::model('\\app\\common\\model\\Orders')->checkIn($this->request->get('number'));
		if($result===true){
			$this->success('修改成功','/orders/indexin');
		}
		$this->error('修改失败');
	}
	 
	 /**
	 * 改变-->入住中状态为-->已完成状态
	 */
	public function getChangeLeavedOrder(){
		$result = Loader::model('\\app\\common\\model\\Orders')->orderEnd($this->request->get('number'));
		if($result===true){
			$this->success('修改成功','/orders/IndexComplete');
		}
		$this->error('修改失败');
	}
	
	/**
	 * 入住用户身份表单
	 */
	public function getIndex(){
	    return view('order/index');
	} 
	 
	  
	/**
	 * 获取手机验证码
	 */ 
	public function postCode(){
		if(!Loader::model('Users')->where('mobile',$this->request->post('mobile'))->find()){
			return json(['status'=>0,'message'=>'注册账户手机号不存在']);
		}
		if(Loader::model('PhoneCode')->Send($this->request->post('mobile'))){
			return json(['status'=>0,'message'=>'手机验证码已发送，请注意查收']);
		}
	} 
	 
	/**
	 * 入住身份验证
	 */ 
	public function postIndex(){
		if(Loader::model('PhoneCode')->Check($this->request->post('code'),session_id()) !== true){
			return $this->error(Loader::model('PhoneCode')->getError());
		}
		$mobile = Loader::model('PhoneCode') -> where(['code' => $this->request->post('code')]) -> value('mobile');
		return $this->success('验证通过','/orders/list?mobile='.$mobile);
	}
	 
	/**
	 * 入住用户订单列表
	 */ 
	public function getList(){
		$user_info = Loader::model('Users') -> get(['mobile' => $this->request->get('mobile')]);
		$order = $this->model->getOrders(['stage' => self::ORDER_PAYMENT_STAGE, 'status' => self::STATE_END, 'camp_id' => $this->camp_id, 'order_user_id' => $user_info->id]);
		return view('order/index_list',['order'=>$order,'user_info'=>$user_info]);
	} 
	 
}






 ?>